﻿using NLog.Fluent;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Net.Sockets;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml;
using System.Collections.Specialized;

namespace Oliving.Common
{
    public static class AppConfigHelper
    {
        public static bool isTest = ConfigurationManager.AppSettings["IsTest"] == "1";


        public static void SetConfig(string key = "name", string val = "App")
        {
            // 修改
            Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            config.AppSettings.Settings[key].Value = val;
            config.Save(ConfigurationSaveMode.Modified);
            ConfigurationManager.RefreshSection("appSettings");
        }
    }

    public static class AppConfigHelperB
    {
        public static string fileName = System.IO.Path.GetFileName(Application.ExecutablePath);
        public static bool addSetting(string key, string value)
        {
            Configuration config = System.Configuration.ConfigurationManager.OpenExeConfiguration(fileName);
            config.AppSettings.Settings.Add(key, value);
            config.Save();
            return true;
        }
        public static string getSetting(string key)
        {
            Configuration config = System.Configuration.ConfigurationManager.OpenExeConfiguration(fileName);
            string value = config.AppSettings.Settings[key].Value;
            return value;
        }
        public static bool updateSeeting(string key, string newValue)
        {
            Configuration config = System.Configuration.ConfigurationManager.OpenExeConfiguration(fileName);
            string value = config.AppSettings.Settings[key].Value = newValue;
            config.Save();
            return true;
        }
    }


    public class ConfigHelper
    {
        private static string _path = String.Format("{0}.config", AppDomain.CurrentDomain.BaseDirectory.ToString() + "XXXX.exe");
        //"AppDomain.CurrentDomain.FriendlyName"

        /// <summary>
        /// 配置文件完整路径
        /// </summary>
        public static string Path
        {
            get { return _path; }
            set { _path = value; }
        }

        public static void SetValue(string key, string value)
        {
            //if (!File.Exists(_path)) return;

            XmlElement xElement2;
            try
            {
                //FileStream fs = new FileStream(_path, FileMode.Open, FileAccess.ReadWrite);

                XmlDocument xDoc = new XmlDocument();
                xDoc.Load(_path);
                //fs.Close();

                XmlNode xNode = xDoc.SelectSingleNode("//appSettings");
                if (xNode != null)
                {
                    XmlElement xElement1 = (XmlElement)xNode.SelectSingleNode(String.Format("//add[@key='{0}']", key));
                    if (xElement1 != null)
                        xElement1.SetAttribute("value", value);
                    else
                    {
                        xElement2 = xDoc.CreateElement("add");
                        xElement2.SetAttribute("key", key);
                        xElement2.SetAttribute("value", value);
                        xNode.AppendChild(xElement2);
                    }
                }
                //fs = new FileStream(_path, FileMode.Open, FileAccess.ReadWrite);
                xDoc.Save(_path);

                //fs.Close();
                xDoc = null;
            }
            catch (Exception e)
            {
            }

        }

        /// <summary>
        /// 获取指定的键的值
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static string GetValue(string key)
        {
            try
            {
                XmlDocument xDoc = new XmlDocument();
                xDoc.Load(_path);

                XmlNode xNode = xDoc.SelectSingleNode("//appSettings");
                if (xNode != null)
                {
                    XmlElement xElement = (XmlElement)xNode.SelectSingleNode(String.Format("//add[@key='{0}']", key));

                    if (xElement != null)
                        return xElement.GetAttribute("value");
                }
            }
            catch (Exception e)
            {
                var dd = Environment.CurrentDirectory.ToString();//获取或设置当前工作目录的完全限定路径
                 
            }
            return string.Empty;
        }


        public static int GetValueInt(string key)
        {
            string val = GetValue(key);

            int intVal = 0;

            int.TryParse(val, out intVal);


            return intVal;
        }


        public static string[] GetLocalIpv4()
        {
            //事先不知道ip的个数，数组长度未知，因此用StringCollection储存
            try
            {
                IPAddress[] localIPs;
                localIPs = Dns.GetHostAddresses(Dns.GetHostName());
                StringCollection IpCollection = new StringCollection();
                foreach (IPAddress ip in localIPs)
                {
                    //根据AddressFamily判断是否为ipv4,如果是InterNetWork则为ipv6
                    if (ip.AddressFamily == AddressFamily.InterNetwork)
                        IpCollection.Add(ip.ToString());
                }
                string[] IpArray = new string[IpCollection.Count];
                IpCollection.CopyTo(IpArray, 0);
                return IpArray;
            }

            catch (Exception ex)
            {
                return null;
            }
            return null;
        }
    }



}
